package com.example.unittestdemo

import android.app.Application
import android.content.Intent
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.testing.FragmentScenario
import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.example.unittestdemo.config.ReadConfig
import com.example.unittestdemo.delegate.ReadLoaderDelegate
import com.example.unittestdemo.entity.MUserInfo
import com.example.unittestdemo.fragment.MainFragment
import com.example.unittestdemo.util.ScreenUtils
import io.mockk.MockKAnnotations
import io.mockk.every
import io.mockk.impl.annotations.MockK
import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.spyk
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.Shadows.shadowOf
import org.robolectric.shadows.ShadowLog
import org.robolectric.shadows.ShadowToast


/**
 * create by liaohailong
 * 2024/4/12 21:29
 * desc:
 */
@RunWith(AndroidJUnit4::class)
class RobolectricTest {

    @MockK
    private lateinit var userInfo: MUserInfo

    @Before
    fun setup() {
        MockKAnnotations.init(this, relaxed = true)
        ShadowLog.stream = System.out
    }

    @Test
    fun testPerson() {
        every { userInfo.id } returns 123
        every { userInfo.name } returns "victor"
        every { userInfo.age } returns 18
        every { userInfo.shortName } answers { callOriginal() }


        assertEquals("123-victor-18", userInfo.shortName)
    }

    @Test
    fun testToast() {
        val scenario = ActivityScenario.launch(MainActivity::class.java)
        scenario.onActivity { activity ->
            val view = activity.findViewById<TextView>(R.id.btn_toast)
            assertEquals("Show Toast", view.text)


            // 1 toast模拟
            val toast = ShadowToast.getLatestToast()
            assertNull(toast)

            view.performClick()
            val toast2 = ShadowToast.getLatestToast()
            assertEquals(Toast.LENGTH_SHORT, toast2.duration)
            assertEquals("Hello World!", ShadowToast.getTextOfLatestToast())
        }
    }

    @Test
    fun testActivityJump() {
        val scenario = ActivityScenario.launch(MainActivity::class.java)
        scenario.onActivity { activity ->
            val view = activity.findViewById<View>(R.id.btn_click)
            view.performClick()

            // 判断是否即将打开下一个 Activity
            val intent = Intent(activity, DetailActivity::class.java)
            val app = ApplicationProvider.getApplicationContext<Application>()
            assertEquals(intent.component, shadowOf(app).nextStartedActivity.component)

        }
    }


    @Test
    fun testFragment() {
        val scenario = FragmentScenario.launch(MainFragment::class.java)
        scenario.onFragment {
            assertNotNull(it.view)

            val tv1 = it.view?.findViewById<TextView>(R.id.tv_hello)
            assertEquals("hello1", tv1?.text)

        }
    }


    @Test
    fun testStaticMethod() {
        mockkStatic(ReadConfig::class)
        every { ReadConfig.getInstance() } returns mockk(relaxed = true)

        mockkStatic(ScreenUtils::class)
        every { ScreenUtils.getAppScreenWidth() } returns 700

        val loader = spyk(ReadLoaderDelegate())

        // 写错一个看看report
        assertEquals("-700", loader.getConfigStr())
    }


}